<?php

/* 
 * 操作日志
 * @2015-12-29 wzl
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class LogController extends AppController
{
    public function __construct() {
        parent::__construct();
        $this->modelObj = new AdminLogModel();
        $this->log_type = '日誌管理';
        $this->title = '日誌管理';
        $this->cookie_key = 'log_key';
    }

    public function view(){
        $this->get_data();        
        $this->display('log');
    }
    
    public function query(){
        $this->get_data();        
        $this->ret['status'] = 0;
        $this->ret['content'] = $this->fetch('log');
        exit(json_encode($this->ret));
    }
    /**
     * 获取数据
     */
    private function get_data(){
        $page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;        
        $where = '';
        $filter['user_name'] = isset($_POST['user_name']) ? $_POST['user_name'] : '';
        $filter['log_type'] = isset($_POST['log_type']) ? $_POST['log_type'] : '';
        if(!empty($filter['user_name'])) {
            $user_id = AdminUserModel::load()->get_user_id($filter['user_name']);
            $where .= ' and user_id = '. $user_id;
        }
        if(!empty($filter['log_type'])) {
            $where .= ' and log_type = ' . "'{$filter['log_type']}')";
        }
        $sort_by = isset($_REQUEST['sort_by']) ? $_REQUEST['sort_by'] : $this->modelObj->keyFields;
        $sort_order = isset($_REQUEST['sort_order']) ? $_REQUEST['sort_order'] : 'DESC';
        $pageObj = new pageModel('admin_log', $where, $this->page_number, 10, $sort_by, $sort_order);        
        $data = $pageObj->pageData($page);           
        $list = $data['pageData'];
        foreach ($list as $key => $row) {
            $user_id = $row['user_id'];
            $data['pageData'][$key]['id'] = $key + 1; 
            $data['pageData'][$key]['user_name'] = AdminUserModel::load()->get_user_name($user_id);
            $data['pageData'][$key]['time_format'] = publicController::load()->friendlyDate($row['log_time']);
            $data['pageData'][$key]['time_format_str'] = publicController::load()->timeToStr($row['log_time']);
        }
        $this->assign('sort_' . $sort_by, $data['sort_img']);
        $this->assign('data', $data);
    }

    public function ajax_get_list() {
        $page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;
        $where = '';
        $pageObj = new pageModel('admin_log', $where, 10, 10, 'log_time');
        $data = $pageObj->pageData($page);        
        if($data) {
            $userObj = new AdminUserModel();
            foreach ($data['pageData'] as $key => $row) {
                $data['pageData'][$key]['log_time'] = date("Y/m/d H:i", $row['log_time']);
                $user_id = $row['user_id'];
                if($user_id > 0) {
                    $data['pageData'][$key]['user_name'] = $userObj->get_user_name($user_id);
                }
            }
            $this->ret['status'] = 0;
            $this->ret['msg'] = $data;
        }
        echo json_encode($this->ret);exit;
    }
    
    public function del() {
        
    }
}

